package com.lagou.demo.controller;

import com.lagou.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouController;
import com.lagou.edu.mvcframework.annotations.LagouRequestMapping;
import com.lagou.edu.mvcframework.annotations.LagouSecurity;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@LagouController
@LagouRequestMapping("/demo")
@LagouSecurity({"zhangsan"})
public class DemoController {


    @LagouAutowired
    private IDemoService demoService;


    /**
     * URL: /demo/query?name=lisi
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/query")
    public String query(HttpServletRequest request, HttpServletResponse response,String name) {
        return demoService.get(name);
    }

    /**
     * 该方法不配置@LagouSecurity注解，默认是Controller类上的用户权限
     * URL: /demo/handle01?username=zhangsan 可以访问,其他不可以访问
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/handle01")
    public void handle01(HttpServletRequest request, HttpServletResponse response,String name) throws Exception {
        response.getWriter().write("---welcome! zhangsan---- ");
    }

    /**
     * 该方法配置了@LagouSecurity注解，方法的权限用户为类+方法上配置的用户权限集合
     * URL: /demo/handle02?username=zhangsan 和 lisi 可以访问,其他不可以访问
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/handle02")
    @LagouSecurity({"lisi"})
    public void handle02(HttpServletRequest request, HttpServletResponse response,String name) throws Exception {
        response.getWriter().write("---welcome! zhangsan or lisi----") ;
    }
}
